<?php

namespace Ycr\Pack\helpers;


/**
 * Class ArrayHelper
 * @package Ycr\Pack\helpers
 * @author ycr <Email: yuncran@126.com>
 */
class ArrayHelper
{
    public static function filter($array, $filters)
    {
        $result = [];
        $excludeFilters = [];

        foreach ($filters as $filter) {
            if (!is_string($filter) && !is_int($filter)) {
                continue;
            }

            if (is_string($filter) && strncmp($filter, '!', 1) === 0) {
                $excludeFilters[] = substr($filter, 1);
                continue;
            }

            $nodeValue = $array; //set $array as root node
            $keys = explode('.', (string)$filter);
            foreach ($keys as $key) {
                if (!array_key_exists($key, $nodeValue)) {
                    continue 2; //Jump to next filter
                }
                $nodeValue = $nodeValue[$key];
            }

            //We've found a value now let's insert it
            $resultNode = &$result;
            foreach ($keys as $key) {
                if (!array_key_exists($key, $resultNode)) {
                    $resultNode[$key] = [];
                }
                $resultNode = &$resultNode[$key];
            }
            $resultNode = $nodeValue;
        }

        foreach ($excludeFilters as $filter) {
            $excludeNode = &$result;
            $keys = explode('.', (string)$filter);
            $numNestedKeys = count($keys) - 1;
            foreach ($keys as $i => $key) {
                if (!array_key_exists($key, $excludeNode)) {
                    continue 2; //Jump to next filter
                }

                if ($i < $numNestedKeys) {
                    $excludeNode = &$excludeNode[$key];
                } else {
                    unset($excludeNode[$key]);
                    break;
                }
            }
        }

        return $result;
    }


    /**
     * 子孙树
     * @param $data
     * @param $parent
     * @param $son
     * @param $pid
     * @return array
     * @author ycr <Email: yuncran@126.com>
     */
    public static function getSubTree($data, $parent = 'parent_id', $son = 'id', $pid = 0)
    {
        $tmp = [];
        foreach ($data as $key => $value) {
            if ($value[$parent] == $pid) {
                $value['child'] = self::getSubTree($data, $parent, $son, $value[$son]);
                $tmp[] = $value;
            }
        }
        return $tmp;
    }


    /**
     * 二维数组分组
     * @param $arr
     * @param $key
     * @return array
     * @author ycr <Email: yuncran@126.com>
     */
    public static function arrayGroupBy($arr, $key)
    {
        $grouped = [];
        foreach ($arr as $k => $value) {
            $grouped[$value[$key]][$k] = $value;
        }
        if (func_num_args() > 2) {
            $args = func_get_args();
            foreach ($grouped as $key => $value) {
                $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
                $grouped[$key] = call_user_func_array('arrayGroupBy', $parms);
            }
        }
        return $grouped;
    }

    /**
     * 二维数组排序
     * @param array $arr
     * @param string $sort
     * @return bool
     * @author ycr <Email: yuncran@126.com>
     */
    public static function arraySort(array $arr, string $sort)
    {
       return array_multisort(array_column($arr, 'sort'), SORT_ASC, $arr);
    }
}